#ifndef __VTOR_SPI_H_
#define __VTOR_SPI_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "vtor_elec_module_config.h"

#ifdef __VTOR_SPI__

typedef enum
{
	VTOR_SPI_STATE_OK       = 0x00U,
	VTOR_SPI_STATE_ERROR    = 0x01U,

}VTOR_SPI_State;


typedef struct
{
	uint8_t cpol; // 其实就是clk时钟线空闲（idle）电平
#ifdef __VTOR_SPI_SLAVE__
	uint8_t rxBuf[256];
	uint8_t txBuf[256];
	uint8_t rxByte;
	uint8_t txByte;
#endif
}VtorSpi;

#define GPIO_LEVEL_LOW		0
#define GPIO_LEVEL_HIGH		1

#define VTOR_SPI_PHASE_1EDGE		0
#define VTOR_SPI_PHASE_2EDGE		1

#define VTOR_SPI_POLARITY_LOW	0
#define VTOR_SPI_POLARITY_HIGH	1


void VtorSpi_Init(void);
void VtorSpi_Delay(uint32_t tick);

void VtorSpi_WriteClk(VtorSpi* spi, uint8_t level);
void VtorSpi_WriteMosi(VtorSpi* spi, uint8_t level);
uint8_t VtorSpi_ReadMiso(VtorSpi* spi);
void VtorSpi_WriteCs(VtorSpi* spi, uint8_t level);

void VtorSpi_SetClkBusy(VtorSpi* spi);
void VtorSpi_SetClkIdle(VtorSpi* spi);

void VtorSpi_WriteRead(VtorSpi* spi, uint8_t* txBuf, uint8_t* rxBuf, uint16_t len);


#ifdef __VTOR_SPI_SLAVE__

void VtorSpiSlave_Init(void);

uint8_t VtorSpiSlave_ReadClk(VtorSpi* spi);
uint8_t VtorSpiSlave_ReadMosi(VtorSpi* spi);
void VtorSpiSlave_WriteMiso(VtorSpi* spi);
uint8_t VtorSpiSlave_ReadCs(VtorSpi* spi);

void VtorSpi_CsHandler(VtorSpi* spi);
void VtorSpi_ClkHandler(VtorSpi* spi);
void VtorSpi_ClkHandler(VtorSpi* spi);

#endif // __VTOR_SPI_SLAVE__


#endif // __VTOR_SPI__

#ifdef __cplusplus
}
#endif

#endif
